package models

import (
	"fmt"
	"github.com/astaxie/beego/orm"
)

type CompanyInfo struct {
	Id   int64
	Companyid string
	Companyname string
	Departments string
	Areas   string
	Coordinate string
}


//增加公司
func AddCompony(company_info *CompanyInfo)(int64,error){
	db := orm.NewOrm()
	id,err := db.Insert(company_info)
	return id,err
}

//根据公司名获取该公司所有部门
func Getalldepartment(companyname string) string {
	o := orm.NewOrm()
	var companyrrows []CompanyInfo
	if _, err := o.Raw("SELECT * FROM company_info WHERE companyname = ?  ", companyname).QueryRows(&companyrrows); err != nil {
		fmt.Println("err")
	}
	departments:= ""
	for _, deprow := range companyrrows {
		departments = deprow.Departments
	}
	return departments
}

//根据公司名获取该公司所有区域
func Getallareas(companyname string)string  {
	o := orm.NewOrm()
	var companyrrows []CompanyInfo
	if _, err := o.Raw("SELECT * FROM company_info WHERE companyname = ?  ", companyname).QueryRows(&companyrrows); err != nil {
		fmt.Println("err")
	}
	areas:= ""
	for _, deprow := range companyrrows {
		areas = deprow.Areas
	}
	return areas
}

//根据公司名获取公司坐标
func Getcoordinate(companyname string)string  {
	o := orm.NewOrm()
	var companyrrows []CompanyInfo
	if _, err := o.Raw("SELECT * FROM company_info WHERE companyname = ?  ", companyname).QueryRows(&companyrrows); err != nil {
		fmt.Println("err")
	}
	coordinate := ""
	for _, deprow := range companyrrows {
		coordinate = deprow.Coordinate
	}
	return coordinate
}







//根据公司id获取该公司名
func GetCompanyname(companyid string) string {
	o := orm.NewOrm()
	var companyrrows []CompanyInfo
	if companyid != "null" {
		if _, err := o.Raw("SELECT * FROM company_info WHERE companyid = ?  ", companyid).QueryRows(&companyrrows); err != nil {
			fmt.Println("err")
		}
	}
	companyname:= ""
	for _, companyrow := range companyrrows {
		companyname = companyrow.Companyname
	}
	return companyname
}


